package com.itany.corejava.day15;

import java.io.*;

public class SerializableUtil {

    //对象写入数组
    public static byte[] encoderObject(Object obj){
        ByteArrayOutputStream baos=null;
        ObjectOutputStream oos=null;
        byte[] b=null;
        try{

            baos=new ByteArrayOutputStream();
            oos=new ObjectOutputStream(baos);

            oos.writeObject(obj);

            b=baos.toByteArray();
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return b;
    }

    //数组读出对象
    public static Object decoderObject(byte[] b){
        ByteArrayInputStream bais=null;
        ObjectInputStream ois=null;
        Object obj=null;

        try{

            bais=new ByteArrayInputStream(b);
            ois=new ObjectInputStream(bais);

            obj=ois.readObject();

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return obj;
    }
}
